<?php

/**
 * @author gbfun <gbfun@163.com>  2018-08-12 20:20
 * http://www.wuweiyou.com
 */

class db_model_address_region
{
    protected $_db_table;   
    protected $_formatter;
    
    protected static $_instance;
    
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();    //by gbfun 又是静态变量
        }
    
        return self::$_instance;
    }
    
    private function __clone()
    {
    }
    
    protected function __construct()
    {
    }

    public function get_db_table()
    {
        if (null === $this->_db_table) {
            $this->_db_table = db_table_address_region::get_instance();
        }
        
        return $this->_db_table;
    }
    
    public function get_formatter()
    {
        if (null === $this->_formatter) {
            $this->_formatter = formatter_address_region::get_instance();
        }
    
        return $this->_formatter;
    }
    
    public function set_rows(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row($row);            
        }
    }
    
    public function set_row(& $row)
    {

    }
    
    public function set_rows_user(& $rows)
    {
        foreach ($rows as &$row) {
            $this->set_row_user($row);
        }
    }
    
    public function set_row_user(& $row)
    {

    }
    
    public function find($id)
    {
        $row = $this->get_db_table()->find($id);
    
        if(!empty($row)){
            $this->set_row($row);
        }
    
        return $row;
    }
    
    public function fetch_row($where)
    {
        $row = $this->get_db_table()->fetch_row($where);
    
        if(!empty($row)){
            $this->set_row($row);
        }
    
        return $row;
    }
    
    public function fetch_all($where = null, $order = null)
    {
        $rows = $this->get_db_table()->fetch_all($where, $order);
    
        $this->set_rows($rows);
    
        return $rows;
    }
    
    public function get_list($where = null, $order = null, $options = array())
    {
        $list = $this->get_db_table()->get_list($where, $order, $options);

        $this->set_rows($list['list']);
    
        return $list;
    }
    
    public function get_simple_list($where = null, $order = null, $options = array())
    {
    
    }    
    
    public function get_region_list($pid, $level, $if_id_key = true)
    {        
        $region_list = array();
        
        $where = array('pid' => $pid, 'level' => $level);
        $order = array('id ASC');
        $region_list_temp = $this->fetch_all($where, $order);
        
        if($if_id_key){
            foreach($region_list_temp as $region){
                $region_list[$region['id']] = $region;
            }
        }else{
            $region_list = $region_list_temp;
        }
    
        return $region_list;
    }
    
    public function get_province_list()
    {
        $pid   = 1;
        $level = 1;       
        $province_list = $this->get_region_list($pid, $level);
    
        return $province_list;
    }
    
    public function get_city_list($province_id)
    {
        $pid   = $province_id;
        $level = 2;
        $city_list = $this->get_region_list($pid, $level);
    
        return $city_list;
    }
    
    public function get_district_list($city_id)
    {
        $pid   = $city_id;
        $level = 3;
        $district_list = $this->get_region_list($pid, $level);
    
        return $district_list;
    }
    
    public function get_street_list($district_id)
    {
        $pid   = $district_id;
        $level = 4;
        $street_list = $this->get_region_list($pid, $level);
    
        return $street_list;
    }
    
    public function check_region($province_id, $city_id, $district_id, $street_id)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
        
        if( (empty($province_id)) || (empty($city_id)) || (empty($district_id)) ){
            $result['status']  = -400;
            $result['message'] = '请选择有效的区域';
            
            return $result;
        }
        
        $check_province_id_result = $this->check_province_id($province_id);
        if($check_province_id_result['status'] < 0){
            return $check_province_id_result;
        }
        
        $check_city_id_result = $this->check_city_id($city_id, $province_id);
        if($check_city_id_result['status'] < 0){
            return $check_city_id_result;
        }
        
        $check_district_id_result = $this->check_district_id($district_id, $city_id);
        if($check_district_id_result['status'] < 0){
            return $check_district_id_result;
        }
        
        $check_street_id_result = $this->check_street_id($street_id, $district_id);  
        if($check_street_id_result['status'] < 0){
            return $check_street_id_result;
        }
        
        $result['status']  = 1;
        $result['message'] = '检查通过';
        
        return $result;
    }
    
    public function check_province_id($province_id)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
    
        $province = $this->find($province_id);
        if(empty($province)){
            $result['status']  = -400;
            $result['message'] = '请选择正确的省';
        }
        
        return $result;
    }
    
    public function check_city_id($city_id, $province_id)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
    
        $city = $this->find($city_id);
        if( (empty($city)) || ($city['pid'] != $province_id) ){
            $result['status']  = -400;
            $result['message'] = '请选择正确的市';
        }
        
        return $result;
    }
    
    public function check_district_id($district_id, $city_id)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
        
        $district = $this->find($district_id);
        if( (empty($district)) || ($district['pid'] != $city_id) ){
            $result['status']  = -400;
            $result['message'] = '请选择正确的区/县';
        }
        
        return $result;
    }
    
    public function check_street_id($street_id, $district_id)
    {
        $result = array('status' => 0, 'message' => '', 'data' => array());
    
        if(empty($street_id)){
            $street_list = $this->get_street_list($district_id);
            if(!empty($street_list)){
                $result['status']  = -400;
                $result['message'] = '请选择街道/镇';
            }
        }else{
            $street = $this->find($street_id);
            if( (empty($street)) || ($street['pid'] != $district_id) ){
                $result['status']  = -400;
                $result['message'] = '请选择正确的街道/镇';
            }
        }
        
        return $result;
    }
}

